<?php

namespace App\Admin\Controllers;

use App\Models\Banners;
use App\Models\BannersThumbs;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;

class BannersThumbsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Banner图册';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BannersThumbs());

        $grid->column('id', __('table.banners_thumbs.id'));
        $grid->column('banners_id', __('table.banners_thumbs.banners_id'))->display(function ($id){
            return Banners::find($id)->location;
        });
        $grid->column('thumbs', __('table.banners_thumbs.thumbs'));
        $grid->column('links', __('table.banners_thumbs.links'));
        $grid->column('status', __('table.banners_thumbs.status'));
        $grid->column('deleted_at', __('Deleted at'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BannersThumbs::findOrFail($id));

        $show->field('id', __('table.banners_thumbs.id'));
        $show->field('banners_id', __('table.banners_thumbs.bannners id'));
        $show->field('thumbs', __('table.banners_thumbs.thumbs'));
        $show->field('links', __('table.banners_thumbs.links'));
        $show->field('status', __('table.banners_thumbs.status'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BannersThumbs());
        // banner数组
        $bannerArr = Banners::get()->toArray();
        $tmp = [];
        foreach ($bannerArr as $item){
            $tmp[$item['id']] = $item['location'];
        }
        $form->select('banners_id', __('table.banners_thumbs.bannners_id'))->options(
            $tmp
        );
        $form->image('thumbs', __('table.banners_thumbs.thumbs'));
        $form->text('links', __('table.banners_thumbs.links'));
        $form->switch('status', __('table.banners_thumbs.status'));

        return $form;
    }
}
